# Replication archive for 
# Coppock, Alexander, Donald P. Green, and Ethan Porter. 2022. 
# Does Digital Advertising Affect Vote Choice? Evidence from a Randomized Field Experiment 
# Research & Politics. 

rm(list = ls())
# setwd() to replication archive

library(tidyverse)
library(estimatr)
library(xtable)

precinct_level <- read_rds("CGP_2022_precinct_level.rds")

# Helper function
eq_test <- function(estimate, std.error, tolerance) {
  
  low_eqbound = tolerance * -1
  high_eqbound = tolerance
  
  t1 <- (estimate - low_eqbound)/std.error
  p1 <- pnorm(t1, lower.tail = FALSE)
  t2 <- (estimate - high_eqbound)/std.error
  p2 <- pnorm(t2, lower.tail = TRUE)
  
  ptost <- max(p1, p2)
  ptost
}


fit_1 <-
  lh_robust(
    dem_two_party_share_2018 ~ treatment,
    clusters = vb_tsmart_zip,
    linear_hypothesis = "treatmentvideo_1 - treatmentvideo_2 = 0",
    data = precinct_level
  )

fit_2 <-
  lh_robust(
    dem_two_party_share_2018 ~ treatment +
      dem_two_party_share_2016_nona + vote_2016_missing +
      dem_two_party_share_2014_nona + vote_2014_missing +
      dem_two_party_share_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    linear_hypothesis = "treatmentvideo_1 - treatmentvideo_2 = 0",
    data = precinct_level
  )


fit_3 <-
  lh_robust(
    dem_two_party_vote_margin_2018 ~ treatment,
    clusters = vb_tsmart_zip,
    linear_hypothesis = "treatmentvideo_1 - treatmentvideo_2 = 0",
    data = precinct_level
  )


fit_4 <-
  lh_robust(
    dem_two_party_vote_margin_2018 ~ treatment + 
      dem_two_party_vote_margin_2016_nona + vote_2016_missing +
      dem_two_party_vote_margin_2014_nona + vote_2014_missing +
      dem_two_party_vote_margin_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    linear_hypothesis = "treatmentvideo_1 - treatmentvideo_2 = 0",
    data = precinct_level
  )


fit_5 <-
  lh_robust(
    vote_total_2018 ~ treatment,
    clusters = vb_tsmart_zip,
    linear_hypothesis = "treatmentvideo_1 - treatmentvideo_2 = 0",
    data = precinct_level
  )


fit_6 <-
  lh_robust(
    vote_total_2018 ~ treatment + 
      vote_total_2016_nona + vote_2016_missing +
      vote_total_2014_nona + vote_2014_missing +
      vote_total_2012_nona + vote_2012_missing,
    clusters = vb_tsmart_zip,
    linear_hypothesis = "treatmentvideo_1 - treatmentvideo_2 = 0",
    data = precinct_level
  )

sds <-
  precinct_level %>%
  pivot_longer(
    c(
      vote_total_2018,
      dem_two_party_share_2018,
      dem_two_party_vote_margin_2018
    ),
    names_to = "outcome"
  ) %>%
  group_by(outcome) %>%
  summarise(
    sd_1.0 = sd(value, na.rm = TRUE),
    sd_0.2 = sd_1.0 * 0.2,
    sd_0.1 = sd_1.0 * 0.1
  )


fit_list <- list(DIM = fit_1, 
                 OLS = fit_2, 
                 DIM = fit_3, 
                 OLS = fit_4, 
                 DIM = fit_5, 
                 OLS = fit_6)

diffs_df <-
  fit_list %>%
  map_df(tidy, .id = "estimator") %>%
  filter(term == "treatmentvideo_1 - treatmentvideo_2 = 0") %>%
  left_join(sds)



diffs_df <-
  diffs_df %>%
  rowwise() %>%
  mutate(
    p_eq_0.2 = eq_test(estimate, std.error, tolerance = sd_0.2),
    eq_0.2 = p_eq_0.2 <= 0.05,
    p_eq_0.1 = eq_test(estimate, std.error, tolerance = sd_0.1),
    eq_0.1 = p_eq_0.1 <= 0.05
  ) %>%
  ungroup %>%
  mutate(
    outcome_name =
      case_when(
        outcome == "dem_two_party_share_2018" ~ "Vote Share",
        outcome == "dem_two_party_vote_margin_2018" ~ "Vote Margin",
        outcome == "vote_total_2018" ~ "Vote Total"
      )
  )


table_df <-
  diffs_df %>%
  select(outcome_name,
         sd_1.0,
         estimator,
         estimate,
         std.error,
         p_eq_0.2,
         p_eq_0.1)


table_df %>%
  xtable(digits = 3) %>%
  print.xtable(
    include.colnames = FALSE,
    include.rownames = FALSE,
    only.contents = TRUE,
    hline.after = c()
  )
